import { Button, message } from 'antd';

import MyTable from '@/components/MyTable';
import { editMessages, findByMessagesPageList } from '@/services/sys/messages';
import { PlusOutlined } from '@ant-design/icons';
import { BetaSchemaForm, ProFormInstance } from '@ant-design/pro-form';
import React, { useRef, useState } from 'react';

const columnDialog = [
  {
    title: '基本信息',
    valueType: 'group',
    columns: [
      {
        title: 'code',
        dataIndex: 'code',
        key: 'code',
        initialValues: 0,
        hideInForm: true,
      },
      {
        title: '汉语',
        dataIndex: 'zhCn',
        key: 'zhCn',
        width: 'md',
      },
      {
        title: '维语',
        dataIndex: 'zhUg',
        key: 'zhUg',
        width: 'md',
      },
    ],
  },
];

const cloneTbale = () => {
  const formRef = useRef<ProFormInstance>();
  const column = [
    {
      title: '编号',
      dataIndex: 'index',
      key: 'index',
      valueType: 'indexBorder',
    },

    {
      title: '汉语',
      dataIndex: 'zhCn',
      key: 'zhCn',
      width: 'md',
    },
    {
      title: '维语',
      dataIndex: 'zhUg',
      key: 'zhUg',
      width: 'md',
    },
    /*{
      title: '关键字',
      dataIndex: 'code',
      width: 'md',
    },*/

    {
      title: '操作',
      width: '164px',
      key: 'option',
      valueType: 'option',
      render: (_, row, index, action) => [
        <a
          key="link"
          onClick={(a) => {
            console.log(row);
            editDialogInfo(row, index);
          }}
        >
          编辑
        </a>,
      ],
    },
  ];
  const [editDialg, setEditDialg] = React.useState(false);
  const [messages, setMessages] = useState<API.Messages>();

  function editDialogInfo(row, index) {
    setMessages(row);
    formRef?.current?.setFieldsValue(row);
    setEditDialg(true);
  }

  function hideEditDialg() {
    setMessages({ code: '' });
    formRef.current?.resetFields();
    setEditDialg(false);
  }

  function openEditDialg() {
    setMessages({ code: '' });
    formRef.current?.resetFields();
    setEditDialg(true);
  }

  // @ts-ignore
  return (
    <div>
      <BetaSchemaForm<API.Messages>
        formRef={formRef}
        initialValues={messages}
        onCancel={() => setEditDialg(false)}
        onVisibleChange={setEditDialg}
        title="字典管理"
        visible={editDialg}
        layoutType="ModalForm"
        columns={columnDialog}
        onFinish={async (values) => {
          values.code = messages?.code;
          console.log(values);
          return new Promise<Boolean>((resolve, reject) => {
            editMessages(values)
              .then(() => {
                message.success('操作成功');
                setEditDialg(false);
                refresh();
                resolve(true);
              })
              .catch(() => {
                message.success('操作失败');
                reject(false);
              });
          });
        }}
      />
      <MyTable
        search={false}
        toolbar={() => [
          <Button
            onClick={() => {
              formRef.current?.resetFields();
              openEditDialg();
            }}
            key="button"
            icon={<PlusOutlined />}
            type="primary"
          >
            新建
          </Button>,
        ]}
        request={findByMessagesPageList}
        columns={column}
      ></MyTable>
    </div>
  );
};

export default cloneTbale;
